package uit.nfcmenu.db.model;

import java.util.Date;
import java.util.Locale;

import uit.nfcmenu.util.CommConstant;
import uit.nfcmenu.util.Utilities;
import uit.nfcmenu.webservice.JSONParser;
import android.content.Context;
import android.util.Log;

import uit.nfcmenu.R;

public class OrderModel implements Comparable<OrderModel> {

	public static final int SORT_BY_NAME = 1;
	public static final int SORT_BY_TABLE = 2;
	public static final int SORT_BY_QUANTITY = 3;
	public static final int SORT_BY_TIME = 4;
	public static int sortMode = SORT_BY_TIME;

	private int id;
	private int itemID;
	private int tableNo;
	private int quantity;
	private String status;
	private String orderTime;
	private String orderUser;
	private String description;
	private String comment;
	private Date date;
	private long mDate;

	private int time;
	private long timescape;
	private int itemName;

	@Override
	public String toString() {
		return "id=" + id + ", status=" + status + ", time="
				+ ((date != null) ? date.toString() : "") + ", des="
				+ description;
	}

	public OrderModel() {
		time = 0;
	}

	public OrderModel(OrderModel model) {
		if (model == null) {
			Log.w(OrderModel.class.getSimpleName(), "error contructor null");
			return;
		}

		time = 0;
		itemID = model.getItemID();
		id = model.getId();
		tableNo = model.getTableNo();
		quantity = model.getQuantity();
		orderTime = model.getOrderTime();
		status = model.getStatus().toUpperCase();
		description = model.getDescription();
		comment = model.getComment();
		date = Utilities.parsefromString(orderTime);
		// TODO
		// if(model.getStatus()==null){
		// model.setStatus(OrderStates.NEW.name());
		// }
	}

	public Date getDate() {
		if (date == null) {
			date = Utilities.parsefromString(orderTime);
			if (date != null) {
				mDate = date.getTime();
			} else {
				mDate = 0;
			}
		}
		return date;
	}

	public void setDate(Date date) {
		this.date = date;
	}

	public void setId(int id) {
		this.id = id;
	}

	public int getId() {
		return id;
	}

	public void setItemID(int itemID) {
		this.itemID = itemID;
	}

	public int getItemID() {
		return itemID;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	public String getStatus() {
		return status;
	}

	public void setTableNo(int tableNo) {
		this.tableNo = tableNo;
	}

	public int getTableNo() {
		return tableNo;
	}

	public void setOrderTime(String orderTime) {
		this.orderTime = orderTime;
		date = Utilities.parsefromString(orderTime);
		if (date != null) {
			mDate = date.getTime();
		} else {
			mDate = 0;
		}
	}

	public String getOrderTime() {
		return orderTime;
	}

	public void setQuantity(int quantity) {
		this.quantity = quantity;
	}

	public int getQuantity() {
		return quantity;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public String getDescription() {
		return description;
	}

	public void setComment(String comment) {
		this.comment = comment;
	}

	public String getComment() {
		return (comment == null) ? " " : comment;
	}

	public String getStatusToShow() {
		if (status != null) {
			return CommConstant.mOrderLanguage[CommConstant.getStatus(status)];
		} else {
			return CommConstant.mOrderLanguage[0];
		}
	}

	public int getStatusIcon(Context context) {
		if (status != null) {
			String[] array = context.getResources().getStringArray(R.array.status_array);
			int kq = 0;
			for (int i = 0; i < array.length; i++) {
				if (status.equalsIgnoreCase(array[i])) {
					kq = i;
					break;
				}
			}
			switch (kq) {
			case CommConstant.STATUS_NEW:
				return R.drawable.ic_status_new_selector;
			case CommConstant.STATUS_COOKING:
				return R.drawable.ic_status_cooking_selector;
			case CommConstant.STATUS_COOKED:
				return R.drawable.ic_status_cooked_selector;
			case CommConstant.STATUS_SERVED:
				return R.drawable.ic_status_served_selector; // FIXME
			case CommConstant.STATUS_DENY:
				return R.drawable.ic_status_cancel_selector;
			case CommConstant.STATUS_DONE:
				return R.drawable.ic_status_done_selector;
			default:
				return R.drawable.ic_status_new_selector;
			}

		} else {
			return R.drawable.ic_status_new_selector;
		}
	}

	// ==========================
	// Order Chef
	// ==========================

	public void update(OrderModel model) {
		setComment(model.getComment());
		setDescription(model.getDescription());
		setItemID(model.getItemID());
		setId(model.getId());
		setOrderTime(model.getOrderTime());
		setQuantity(model.getQuantity());
		setStatus(model.getStatus().toUpperCase());
		setTableNo(model.getTableNo());
	}

	public int getTime() {
		return time;
	}

	public void setTime(int time) {
		this.time = time;
	}

	public String getTimeRemain() {
		boolean flag = false;
		int temp = time;
		if (time < 0) {
			temp = Math.abs(time);
			flag = true;
		}
		int minutes = temp / 60;

		String result = "";
		if (minutes < 10) {
			result = result + "0" + minutes + ":";
		} else {
			result = result + minutes + ":";
		}
		// if (second < 10) {
		// result = result + "0" + second;
		// } else {
		// result = result + second;
		// }
		if (flag) {
			result = "-" + result;
		}
		return result;
	}

	public void parseDate() {
		date = Utilities.parsefromString(orderTime);
	}

	public String getOrderUser() {
		return orderUser;
	}

	public void setOrderUser(String orderUser) {
		this.orderUser = orderUser;
	}

	@Override
	public int compareTo(OrderModel another) {
		// sort by name
		if (sortMode == SORT_BY_NAME) {
			String language = Locale.getDefault().getDisplayLanguage();
			String name = JSONParser.getStringByLanguage(
					CommConstant.item.get(itemID), language);

			String anotherName = JSONParser.getStringByLanguage(
					CommConstant.item.get(another.itemID), language);

			return name.compareTo(anotherName);
		}

		// sort by quantity
		if (sortMode == SORT_BY_QUANTITY) {
			return (quantity - another.quantity);
		}

		if (sortMode == SORT_BY_TIME) {
			if (getDate() != null && another.getDate() != null) {
				return getDate().compareTo(another.getDate());
			}
			return 0;
		}

		// default sort by table
		return (tableNo - another.tableNo);
	}

	public long getmDate() {
		return mDate;
	}

	public void setmDate(long mDate) {
		this.mDate = mDate;
	}
}